<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    // 如果A的pid == B的id
    // A是B的下属

    // 如果A的pid == B的pid
    // A和B是同事

    var data = [
      { id: "01", name: "张大大", pid: "", job: "项目经理" },
      { id: "02", name: "小亮", pid: "01", job: "产品leader" },
      { id: "03", name: "小美", pid: "01", job: "UIleader" },
      { id: "04", name: "老马", pid: "01", job: "技术leader" },
      { id: "05", name: "老王", pid: "01", job: "测试leader" },
      { id: "06", name: "老李", pid: "01", job: "运维leader" },
      { id: "07", name: "小丽", pid: "02", job: "产品经理" },
      { id: "08", name: "大光", pid: "02", job: "产品经理" },
      { id: "09", name: "小高", pid: "03", job: "UI设计师" },
      { id: "10", name: "小刘", pid: "04", job: "前端工程师" },
      { id: "11", name: "小华", pid: "04", job: "后端工程师" },
      { id: "12", name: "小李", pid: "04", job: "后端工程师" },
      { id: "13", name: "小赵", pid: "05", job: "测试工程师" },
      { id: "14", name: "小强", pid: "05", job: "测试工程师" },
      { id: "15", name: "小涛", pid: "06", job: "运维工程师" }
    ]

    // 问题1. 找出 与 小刘 处于统一领导下的同事 
    function f(data1, name) {
      // 找到小刘的index
      const index = data1.findIndex(item => item.name === name)
      // 存储小刘的pid
      const pid = data1[index].pid
      // 循环遍历数组找出小刘同一领导的同事
      const arr = data1.filter(item => item.pid === pid)
      return arr
    }
    const arr1 = f(data, '小刘')
    console.log(arr1)

    // 问题2. 找出 小亮 的所有下属 
    function f2(data1, name) {
      // 找到小亮的下标
      const index = data1.findIndex(item => item.name === name) 
      // 存储小亮的id
      const id = data1[index].id
      // 循环遍历数组找出小亮的所有下属
      const arr = data1.filter(item => item.pid === id)
      return arr
    }
    const arr2 = f2(data, '小亮')
    console.log(arr2)
  </script>
</body>

</html>